package cn.iocoder.yudao.module.crossborder.platform.service.platformconfig;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformconfig.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformconfig.PlatformConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformconfig.PlatformConfigMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商平台配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformConfigServiceImpl implements PlatformConfigService {

    @Resource
    private PlatformConfigMapper platformConfigMapper;

    @Override
    public Long createPlatformConfig(PlatformConfigSaveReqVO createReqVO) {
        // 插入
        PlatformConfigDO platformConfig = BeanUtils.toBean(createReqVO, PlatformConfigDO.class);
        platformConfigMapper.insert(platformConfig);

        // 返回
        return platformConfig.getId();
    }

    @Override
    public void updatePlatformConfig(PlatformConfigSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformConfigExists(updateReqVO.getId());
        // 更新
        PlatformConfigDO updateObj = BeanUtils.toBean(updateReqVO, PlatformConfigDO.class);
        platformConfigMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformConfig(Long id) {
        // 校验存在
        validatePlatformConfigExists(id);
        // 删除
        platformConfigMapper.deleteById(id);
    }

    @Override
        public void deletePlatformConfigListByIds(List<Long> ids) {
        // 删除
        platformConfigMapper.deleteByIds(ids);
        }


    private void validatePlatformConfigExists(Long id) {
        if (platformConfigMapper.selectById(id) == null) {
            throw exception(PLATFORM_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public PlatformConfigDO getPlatformConfig(Long id) {
        return platformConfigMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformConfigDO> getPlatformConfigPage(PlatformConfigPageReqVO pageReqVO) {
        return platformConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public PlatformConfigDO getPlatformConfigByCode(String code) {
        return platformConfigMapper.selectByCode(code);
    }

}